/**
 * Controller
 *
 * new Controller({
 *    container : "#main"
 * });
 * ------------------------------------
 *
 * API:
 *
 * loadProcess(datas)
 * parseProcess2XML(psNode)
 *
 */
(function(global){

    'use strict';

    const logger = WorkflowUtil.newLogger("WorkflowApp/Controller");

    let Controller = function (setting) {

        //坐标计算器
        this.autoLayout = new global.AutoLayout(false, true);

        if (WorkflowUtil.isDebug()) {
            // 调试模式
            //this.autoLayout.setIgnoreDelete(true);
            //this.autoLayout.setMergeSplitAndJoin(false);
        }

        this.model = new global.Model();
        this.view = new global.View({
            container: setting.container,
            model: this.model
        });
    };


    /**
     * 初始化
     * @param datas
     *
     * datas = {
     *    processXml : "",//流程xml
     *    caseLogXml : "",//caseLog
     *    caseWorkitemLogXml : "",//workitem状态信息
     * }
     */
    Controller.prototype.loadProcess = function(datas){

        const start = new Date();

        this.model.parseProcessFromXML({
            processXml : datas.processXml,
            caseLogXml : datas.caseLogXml,
            caseWorkitemLogXml : datas.caseWorkitemLogXml
        });

        const process = this.model.getProcess();
        const caseLog = this.model.getCaseLog();

        // 计算节点状态
        this._setNodeState(process, caseLog);
        
        
        //计算位置
        this.autoLayout.calculateLocation(process);
        
        //画图
        this.view.setModel(this.model);
        
        this.view.draw();
        
        //初始化菜单

        const end = new Date();

        logger.debug("流程加载耗时：" + (end - start) + "ms");
    };


    /**
     * 获取process 的 xml 信息
     */
    Controller.prototype.parseProcess2XML = function(psNode, docRoot){

        const process = this.model.getProcess();

        process.toXML(psNode, docRoot);
    };
    
    
    /**
     * 设置流程节点的状态
     */
    Controller.prototype._setNodeState = function(process, caseLog){
        
        if(!caseLog){
            // 没有case数据
            return;
        }

        const stepList = caseLog.getStepList();

        let i = 0, len = stepList.length;
        for (; i < len; i++){

            const step = stepList[i];

            const frameList = step.getFrameList();

            for (let j = 0; j < frameList.length; j++){

                const frame = frameList[j];

                const node = process.getNode(frame.getNodeId());
                if (node != null){
                    
                    if(node.getState() !== WorkflowConstant.nodeState.STATE_STOP
                            || frame.getState() === WorkflowConstant.nodeState.STATE_READY){
                        node.setState(frame.getState());
                    }
                }
            }
        }
    };
    

    global.Controller = Controller;

})(WorkflowApp);
